package com.app.service.impl;

import com.app.mapper.CategoryMapper;
import com.app.model.CategoryModel;
import com.app.service.CategoryService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Page<CategoryModel> categoryPageList(int page, int limit, String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name).orderByAsc("id").ne("pid", 0).orderByDesc("sort");
        Page<CategoryModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<CategoryModel> categoryList = categoryMapper.selectPage(pages, queryWrapper);
        return categoryList;
    }

    @Override
    public List<CategoryModel> selectPerTree(String keyWord) {//全查配置
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (StringTool.isNotEmpty(keyWord)) {
            queryWrapper.like("name", keyWord);
        }
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public List<CategoryModel> getListByPid() {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").ne("pid", 0).eq("status", 1);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public CategoryModel getPersonCate(String name) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").eq("name", name);
        CategoryModel modelList = categoryMapper.selectOne(queryWrapper);
        return modelList;
    }

    @Override
    public List<CategoryModel> getCategoryByPid(Integer pid) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name").eq("pid", pid);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }

    @Override
    public void insert(CategoryModel model) {
        categoryMapper.insert(model);
    }

    @Override
    public CategoryModel HomeCount() {
        return null;
    }


    @Override
    public void update(CategoryModel model) {
        categoryMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<CategoryModel> getAll() {
        List<CategoryModel> modelList = categoryMapper.selectList(null);
        return modelList;
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public CategoryModel selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public CategoryModel getCategoryById(int id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public CategoryModel getCatByPidAndId(int pid, int id) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "pid", "name").eq("pid", pid).eq("id", id);
        CategoryModel model = categoryMapper.selectOne(queryWrapper);
        if (model == null) {
            return null;
        } else {
            return model;
        }
    }

    @Override
    public List<CategoryModel> getCatByP(int pid) {
        QueryWrapper<CategoryModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        List<CategoryModel> modelList = categoryMapper.selectList(queryWrapper);
        return modelList;
    }
}
